package com.anmong.cloud.cloudlog.mq;

import com.anmong.cloud.cloudapi.entity.SysOperateLog;
import com.anmong.cloud.cloudapi.mapper.SysOperateLogMapper;
import com.anmong.cloud.cloudcommon.util.IDUtil;
import com.anmong.cloud.mq.constant.MqEnum;
import com.anmong.cloud.mq.dto.AccessLogDTO;
import com.anmong.cloud.mq.dto.SysOperateLogDTO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


@Component
@Slf4j
public class MqLogListener {

    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private SysOperateLogMapper sysOperateLogMapper;


    @Service
    @RocketMQMessageListener(consumerGroup = MqEnum.GROUP_API_ACCESS_LOG, topic = MqEnum.TOPIC_LOG,selectorExpression = MqEnum.TAG_API_ACCESS_LOG,
            consumeMode = ConsumeMode.ORDERLY)
    public class ApiAccessLogConsumer implements RocketMQListener<String>,RocketMQPushConsumerLifecycleListener {
        @Override
        public void onMessage(String message) {
            if (StringUtils.isNotEmpty(message)){
                log.info("收到访问日志正常消息:{}", message);
                try {
                    AccessLogDTO dto = objectMapper.readValue(message,AccessLogDTO.class);
                    String url = dto.getUrl();
                    //消息存入es中
                } catch (JsonProcessingException e) {
                    log.error("访问日志消息结构错误,exception:{},消息内容:{}", e.getMessage(),message);
                }
            }
            else {
                log.error("收到访问日志错误消息:消息内容为空");
            }
        }

        @Override
        public void prepareStart(DefaultMQPushConsumer consumer) {
            // 设置最大重试次数
            consumer.setMaxReconsumeTimes(3);
            // 如下，设置其它consumer相关属性
            //consumer.setPullBatchSize(16);
        }


    }

    @Service
    @RocketMQMessageListener(consumerGroup = MqEnum.GROUP_SYS_OPERATE_LOG, topic = MqEnum.TOPIC_LOG,selectorExpression = MqEnum.TAG_SYS_OPERATE_LOG,
            consumeMode = ConsumeMode.ORDERLY)
    public class SysOperateLogConsumer implements RocketMQListener<String>,RocketMQPushConsumerLifecycleListener {
        @Override
        public void onMessage(String message) {
            if (StringUtils.isNotEmpty(message)){
                log.info("收到操作日志正常消息:{}", message);
                SysOperateLog sysOperateLog = new SysOperateLog();
                SysOperateLogDTO dto = null;
                try {
                    dto = objectMapper.readValue(message,SysOperateLogDTO.class);
                } catch (JsonProcessingException e) {
                    log.error("操作日志消息结构错误,exception:{},消息内容:{}", e.getMessage(),message);
                }
                BeanUtils.copyProperties(dto,sysOperateLog);
                sysOperateLog.setId(IDUtil.get());
                sysOperateLogMapper.insert(sysOperateLog);
            }
            else {
                log.error("收到操作日志错误消息:消息内容为空");
            }

        }

        @Override
        public void prepareStart(DefaultMQPushConsumer consumer) {
            // 设置最大重试次数
            consumer.setMaxReconsumeTimes(3);
            // 如下，设置其它consumer相关属性
            //consumer.setPullBatchSize(16);
        }
    }
}
